package paa.simulator.model;

import java.util.Collection;
import java.util.Iterator;

import paa.simulator.grafo.PessoaVertex;
import paa.simulator.grafo.simple.PessoaEdge;
import paa.simulator.grafo.simple.UndirectedSimpleGraph;

public class ContagionThreshold extends AbstractModel {

	private final double payoffBuy;

	public ContagionThreshold(double payoffBuy) {
		this.payoffBuy = payoffBuy;
	}
	
	public void initiateGraph(UndirectedSimpleGraph<PessoaVertex> graph) {
	}
	
	public boolean executeTurn(UndirectedSimpleGraph<PessoaVertex> graph) {
		boolean changedOpinions = false;
		
		for (PessoaVertex pv : graph.vertexSet()) {
			if (!pv.isBuying()) {
				Collection<PessoaEdge<PessoaVertex>> edges = graph.edgesOf(pv);
				
				int qtdVBuying = 0;
				int qtdVNotBuying = 0;
	
				Iterator<PessoaEdge<PessoaVertex>> it = edges.iterator();
				while (it.hasNext()) {
					PessoaEdge<PessoaVertex> next = it.next();
					PessoaVertex other = next.getRelation(pv);
					
					if (other.isBuying()) {
						qtdVBuying++;
					} else {
						qtdVNotBuying++;
					}
				}
				
				if (qtdVBuying * payoffBuy > qtdVNotBuying * (1 - payoffBuy)) {
					setAsBuying(pv, graph);
					changedOpinions = true;
				}
			}
		}
		
		return changedOpinions;
	}
}
